package com.kyxx.topics.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.kyxx.topics.R;
import com.kyxx.topics.bean.MemberAddCardBean;
import com.kyxx.topics.net.UrlUtils;
import com.kyxx.topics.view.CircleImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;


public class MemberAddCardAdapter extends RecyclerView.Adapter<MemberAddCardAdapter.ViewHolder> {
    private Context mContext;
    private List<MemberAddCardBean> mDatas;
    private LayoutInflater mInflater;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options;
    private OnItemClickLitener mOnItemClickLitener;
    public MemberAddCardAdapter(Context mContext, List<MemberAddCardBean> mDatas) {
        this.mContext = mContext;
        this.mDatas = mDatas;
        this.mInflater = LayoutInflater.from(mContext);
        options = new DisplayImageOptions.Builder()
                // 设置图片在下载期间显示的图片
                .showImageOnLoading(R.mipmap.image_default)
                // 设置图片Uri为空或是错误的时候显示的图片
                .showImageForEmptyUri(R.mipmap.image_default)
                // 设置图片加载/解码过程中错误时候显示的图片
                .showImageOnFail(R.mipmap.image_default)
                .cacheInMemory(true)
                // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)
                // 设置下载的图片是否缓存在SD卡中
                .bitmapConfig(Bitmap.Config.RGB_565)// 设置图片的解码类型
                // .displayer(new RoundedBitmapDisplayer(9999))
                .resetViewBeforeLoading(false)
                .build();
    }

    @Override
    public MemberAddCardAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.item_member_add, parent, false));
    }

    @Override
    public void onBindViewHolder(final MemberAddCardAdapter.ViewHolder holder, final int position) {
        final MemberAddCardBean bean = mDatas.get(position);
        holder.tv_member_card_name.setText(bean.getType_name());
        imageLoader.displayImage(UrlUtils.BASEHTTP+bean.getType_logo(), holder.cv_member_add_icon, options);
        // 如果设置了回调，则设置点击事件
        if (mOnItemClickLitener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v)
                {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });

        }
    }

    @Override
    public int getItemCount() {
        return mDatas != null ? mDatas.size() : 0;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_member_card_name;
        CircleImageView cv_member_add_icon;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_member_card_name = (TextView) itemView.findViewById(R.id.tv_member_card_name);
            cv_member_add_icon = (CircleImageView) itemView.findViewById(R.id.cv_member_add_icon);
        }
    }
    public interface OnItemClickLitener{
        void onItemClick(View view, int position);
    }

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener){
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
}
